package com.wlyuan.open.controller;


import com.alibaba.fastjson.JSONObject;
import com.wlyuan.ere.api.*;
import com.wlyuan.ere.common.dto.*;
import com.wlyuan.ere.common.filter.OrderFilter;
import com.wlyuan.ere.common.filter.OrderPositionFilter;
import com.wlyuan.ere.common.vo.OrderTransportVO;
import com.wlyuan.web.claim.ClaimUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "轨迹相关")
@Validated
@RequestMapping("/api/track")
@RestController
public class OpenTrackController {

    @DubboReference(check = false)
    private OrderTrackService orderTrackService;
    @DubboReference(check = false)
    private TmsThirdTrackStandService tmsThirdTrackStandService;
    @DubboReference(check = false)
    private TmsDevicesStandService tmsDevicesStandService;
    @DubboReference(check = false)
    private ErePlatFormService erePlatFormService;
    @DubboReference(check = false)
    private EreService ereService;


    @ApiOperation(value = "轨迹页面--获取坐标间预计距离和时间")
    @GetMapping("/getRouteMatrix/{origins}/{destinations}")
    public JSONObject getRouteMatrix(@PathVariable @NotNull String origins,
                                     @PathVariable @NotNull String destinations) {
        return orderTrackService.getRouteMatrix(origins, destinations);
    }

    @ApiOperation(value = "轨迹页面--获取天气信息")
    @GetMapping("/getWeather/{longitude}/{latitude}")
    public JSONObject getWeather(@PathVariable @NotNull String longitude, @PathVariable @NotNull String latitude) {
        return orderTrackService.getWeather(longitude, latitude);
    }

    @ApiOperation(value = "轨迹页面--获取订单节点信息")
    @PostMapping(value = "/orderFlow")
    public List<OrderTrackFlowDTO> orderFlow(@Valid @RequestBody OrderFilter orderFilter) {
        return orderTrackService.orderFlow(orderFilter);
    }

    @ApiOperation(value = "轨迹页面--获取设备定位集合")
    @PostMapping(value = "/position")
    public OrderTransportVO getOrderPosition(@Valid @RequestBody OrderFilter orderFilter) {
        orderFilter.setTenantId(ClaimUtils.get().getTenantId());
        return orderTrackService.getOrderPosition(orderFilter);
    }

    @ApiOperation(value = "轨迹页面--获取设备当前位置:未到达:type = 0,time=预计到货时间;已到达:type = 1,time=预计到货时间;已签收:type= 2,time=签收时间;location:收货地坐标")
    @GetMapping(value = "/position/{devCode}/{location}/{time}/{type}")
    public JSONObject getCurrentPosition(@PathVariable String devCode, @PathVariable String location,
                                         @PathVariable String time, @PathVariable Integer type) {
        return orderTrackService.currentPosition(devCode, location, time, type);
    }

    @ApiOperation(value = "轨迹页面--获取设备当前位置、电量(无预警信息)")
    @PostMapping(value = "/orderPosition")
    public TmsDevicesDTO orderPosition(@Valid @RequestBody OrderPositionFilter orderPositionFilter) {
        return ereService.getByDevCode(orderPositionFilter.getDevCode(), orderPositionFilter.getSignTime());
    }

    @ApiOperation(value = "轨迹页面--快递节点查询")
    @PostMapping("/getExpress")
    public List<ExpressNodeDTO> getExpress(@RequestBody ExpressSearchDTO expressSearchDTO) {
        return erePlatFormService.getExpress(expressSearchDTO.getCode(), expressSearchDTO.getPhone());
    }

    /**
     * 获取实时定位
     */
    @ApiOperation(value = "轨迹页面--获取订单定位信息(包含补充飞机火车时) 订单id,设备号,收货地坐标")
    @GetMapping(value = "/getLocation/{orderId}/{devCode}/{toLocation}")
    public OrderLocationDTO getLocation(@PathVariable Long orderId, @PathVariable String devCode,
                                        @PathVariable String toLocation) {
        return orderTrackService.getLocation(orderId, devCode, toLocation);
    }

//    @ApiOperation(value = "轨迹页面--飞机、火车行驶轨迹查询 type 1:飞机,2 火车")
//    @GetMapping("/getThirdTrack/{type}/{code}")
//    public TmsThirdTrackStand getThirdTrack(@PathVariable("type") Integer type, @PathVariable("code") String code) {
//        TrackFilter filter = new TrackFilter();
//        filter.setCode(code);
//        filter.setDate(new Date());
//        filter.setType(type);
//        return erePlatFormService.getThirdTrack(filter);
//    }
}
